<?php
/**
 * Created by.
 * CaoMing
 * 2019-11-30
 * 23:43
 */
class Node
{
    public $value;
    public $left;
    public $right;

}

/**
 * PHP用非递归方式实现广度优先遍历二叉树
 * Class Bt
 */
class Bt
{
    protected $root;
    public function __construct()
    {
        $this->root = null;
    }

    public function aaa($root)
    {
        if($root == null){
            return false;
        }
        $node = $root;
        $queue = [];
        array_push($queue,$node);// 根节点入队
        while(!empty($queue)){// 持续输出节点，直到队列为空
            $node = array_shift($queue);
            echo $node->value.'<br/>';//出队
            // 左节点先入队
            if ($node->left != null) {
                array_push($queue, $node->left);
            }
            // 然后右节点入队
            if ($node->right != null) {
                array_push($queue, $node->right);
            }
        }

    }
}
$a = new Node();
$b = new Node();
$c = new Node();
$d = new Node();
$e = new Node();
$f = new Node();
$a->value = 'a';
$b->value = 'b';
$c->value = 'c';
$d->value = 'd';
$e->value = 'e';
$f->value = 'f';

$a->left  = $b;
$a->right = $c;
$b->left  = $d;
$b->right = $e;
$c->left  = $f;
$bt = new Bt();
$bt->aaa($a);

